package com.block.product.service.union;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.service.LogFactory;
import com.block.common.util.LocalDateUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.ProductInfo;
import com.block.product.service.IUnionProductService;
import com.block.product.service.union.utils.ssd.RequestUtil51GJJ;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 橙亿
 */
@Service
public class ChengYiProductServiceImpl implements IUnionProductService {

    private static Logger log = LogFactory.getCommLog();

    @Override
    public ResponseVo matchUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        String requestMatchUrl = productInfo.getChannelMatchUrl();
        String md5Mobile = SecureUtil.md5(userInfoVo.getMobile()).toLowerCase(Locale.ROOT);


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("app_key",productInfo.getChannelCode());
        paramMap.put("user_md5", md5Mobile);

        List<String> list = JSON.parseArray(JSON.toJSONString(paramMap.keySet()), String.class);
        Collections.sort(list);

        StringBuilder stringBuilder = new StringBuilder();
        for (String key : list) {
            stringBuilder.append(key).append("=").append(paramMap.get(key)).append("&");
        }
        String substring = stringBuilder.substring(0, stringBuilder.length() - 1) + productInfo.getPubKey();

        paramMap.put("sign", SecureUtil.md5(substring).toLowerCase());

        log.info("【产品联登】【橙亿】撞库请求【REQUEST_URL】{}【bizMap】{}", requestMatchUrl, JSON.toJSONString(paramMap));
        HttpRequest httpRequest = HttpUtil.createPost(requestMatchUrl);
        httpRequest.form(paramMap);
        String resultStr = httpRequest.execute().body();
        log.info("【产品联登】【橙亿】撞库完成【resultStr】{}", resultStr);


        JSONObject jsonObject = JSON.parseObject(resultStr);
        Integer status = jsonObject.getInteger("status");
        if (status.equals(1)) {
            return new ResponseVo(AppRspCodeConstant.SUCCESS);
        }
        return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
    }

    @Override
    public ResponseVo registerUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        String requestUrl = productInfo.getChannelRegisterUrl();

        long timestamp = System.currentTimeMillis();


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("app_key",productInfo.getChannelKey());
        paramMap.put("mobile", userInfoVo.getMobile());
        paramMap.put("platform", 1);
        paramMap.put("timestamp", timestamp);

        List<String> list = JSON.parseArray(JSON.toJSONString(paramMap.keySet()), String.class);
        Collections.sort(list);

        StringBuilder stringBuilder = new StringBuilder();
        for (String key : list) {
            stringBuilder.append(key).append("=").append(paramMap.get(key)).append("&");
        }
        String substring = stringBuilder.substring(0, stringBuilder.length() - 1) + productInfo.getChannelSecret();
        paramMap.put("sign", SecureUtil.md5(substring).toLowerCase());



        log.info("【产品联登】【橙亿】联登请求【requestUrl】{}【paramMap】{}", requestUrl,  JSON.toJSONString(paramMap));
        HttpRequest httpRequest = HttpUtil.createPost(requestUrl);
        httpRequest.form(paramMap);
        String resultStr = httpRequest.execute().body();
        log.info("【产品联登】【橙亿】联登完成【result】{}", resultStr);

        JSONObject jsonObject = JSON.parseObject(resultStr);
        Integer status = jsonObject.getInteger("status");
        String info = jsonObject.getString("info");
        if (status.equals(1)) {
            JSONObject data = jsonObject.getJSONObject("data");
            Integer code = data.getInteger("code");
            if (code.equals(1)){
                String skipUrl = data.getString("skip_url");
                JSONObject resultData = new JSONObject();
                resultData.put("productUrl", skipUrl);
                return new ResponseVo(AppRspCodeConstant.SUCCESS, resultData);
            }
        }
        return new ResponseVo(AppRspCodeConstant.THRID_FAILED, info, "");
    }





}
